/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.sunspotworld.demo;

import com.sun.spot.core.resources.Resources;
import com.sun.spot.ieee_802_15_4_radio.IRadioPolicyManager;
import com.sun.spot.multihop.io.j2me.radiogram.Radiogram;
import com.sun.spot.multihop.io.j2me.radiogram.RadiogramConnection;
import java.io.IOException;
import javax.microedition.io.Connector;

/**
 *
 * @author me
 */
public class RadioListener extends Thread {
    private IRadioPolicyManager rpm = (IRadioPolicyManager)Resources.lookup(IRadioPolicyManager.class);
    private AirTextDemo demo;

    public RadioListener(int channel, AirTextDemo atd) {
        rpm.setChannelNumber(channel);
        demo = atd;
    }
    
    public void run() {
        RadiogramConnection rConn = null;
        Radiogram rrg;
        try {
            rConn = (RadiogramConnection)Connector.open("radiogram://:45");
            rConn.setMaxBroadcastHops(0);
            rrg = (Radiogram)rConn.newDatagram(rConn.getMaximumLength());
            while (true) {
                rrg.reset();
                rConn.receive(rrg);
                System.out.println("New String Received");
                demo.setDisplayString(rrg.readUTF());
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

}
